package com.casic.ops.configs;


import com.casic.ops.handler.pipeline.PipelineExecuteLoggerWebSocketHandler;
import com.casic.ops.handler.pipeline.PipelineExecuteWebSocketHandler;
import com.casic.ops.handler.pipeline.WebSocketInterceptor;
import com.casic.ops.handler.sftp.notify.FileTransferNotifyHandler;
import com.casic.ops.handler.tail.TailFileHandler;
import com.casic.ops.handler.terminal.TerminalMessageHandler;
import com.casic.ops.interceptor.FileTransferNotifyInterceptor;
import com.casic.ops.interceptor.TailFileInterceptor;
import com.casic.ops.interceptor.TerminalAccessInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;

/**
 * @ClassName ops-platform
 * @Description websocket 配置
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private TerminalMessageHandler terminalMessageHandler;

    @Resource
    private TerminalAccessInterceptor terminalAccessInterceptor;

    @Resource
    private TailFileHandler tailFileHandler;

    @Resource
    private TailFileInterceptor tailFileInterceptor;

    @Resource
    private FileTransferNotifyHandler fileTransferNotifyHandler;

    @Resource
    private FileTransferNotifyInterceptor fileTransferNotifyInterceptor;

    @Resource
    private PipelineExecuteLoggerWebSocketHandler pipelineExecuteLoggerWebSocketHandler;

    @Resource
    private PipelineExecuteWebSocketHandler pipelineExecuteWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(terminalMessageHandler, "/ops/keep-alive/machine/terminal/{token}")
                .addInterceptors(terminalAccessInterceptor)
                .setAllowedOrigins("*");
        webSocketHandlerRegistry.addHandler(tailFileHandler, "/ops/keep-alive/tail/{token}")
                .addInterceptors(tailFileInterceptor)
                .setAllowedOrigins("*");
        webSocketHandlerRegistry.addHandler(fileTransferNotifyHandler, "/ops/keep-alive/sftp/notify/{token}")
                .addInterceptors(fileTransferNotifyInterceptor)
                .setAllowedOrigins("*");
        webSocketHandlerRegistry.addHandler(pipelineExecuteWebSocketHandler,"/pipeline")//获取流水线节点执行状态推送
                .addHandler(pipelineExecuteLoggerWebSocketHandler,"/logger")//推送节点执行实时日志
                .addInterceptors(new WebSocketInterceptor())
                .setAllowedOrigins("*");

    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}
